﻿using BilibiliDM_PluginFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Tea;

namespace DanmuMuyuMonitor
{
    public class Class1 : BilibiliDM_PluginFramework.DMPlugin
    {
        AlibabaCloud.SDK.Iot20180120.Client mClient;
        public Class1()
        {
            this.Connected += Class1_Connected;
            this.Disconnected += Class1_Disconnected;
            this.ReceivedDanmaku += Class1_ReceivedDanmaku;
            this.ReceivedRoomCount += Class1_ReceivedRoomCount;
            this.PluginAuth = "我在等你回复可你没回";
            this.PluginName = "弹幕木鱼监视器";
            this.PluginCont = "example@example.com";
            this.PluginVer = "v0.0.1";
        }


        private void Class1_ReceivedRoomCount(object sender, BilibiliDM_PluginFramework.ReceivedRoomCountArgs e)
        {

        }

        private void Class1_ReceivedDanmaku(object sender, BilibiliDM_PluginFramework.ReceivedDanmakuArgs e)
        {
            if (e.Danmaku.MsgType == MsgTypeEnum.Comment)
            {
                if (e.Danmaku.CommentText.Equals( "功德"))
                {
                    //往云平台发送控制指令
                    AlibabaCloud.SDK.Iot20180120.Models.InvokeThingServiceRequest request = new AlibabaCloud.SDK.Iot20180120.Models.InvokeThingServiceRequest
                    {
                        Args = "{\"Count\":1}",
                        Identifier = "HitMuyu",
                        // 物联网平台实例ID。
                        IotInstanceId = "iot-06z00bud5bhfi0g",
                        // 产品ProductKey。
                        ProductKey = "k0re8zajZkT",
                        DeviceName = "Esp8266",
                        Qos = 0,
                    };
                    //通过client对象获得对应request的响应对象response。
                    try
                    {
                        AlibabaCloud.SDK.Iot20180120.Models.InvokeThingServiceResponse response = mClient.InvokeThingService(request);
                        Console.WriteLine("publish message result: " + response.Body.Success);
                        Console.WriteLine(response.Body.Code);
                        Console.WriteLine(response.Body.ErrorMessage);
                    }
                    catch (TeaException error)
                    {
                        Console.WriteLine(error.Code);
                        Console.WriteLine(error.Message);
                    }
                    catch (Exception _error)
                    {
                        Console.WriteLine(_error.Message);
                        Console.WriteLine(_error.StackTrace);
                    }
                }
                this.Log("Muyu ReceivedDanmaku" + e.Danmaku.CommentText);
                this.AddDM("Muyu ReceivedDanmaku" + e.Danmaku.CommentText , true);
            }
        }

        private void Class1_Disconnected(object sender, BilibiliDM_PluginFramework.DisconnectEvtArgs e)
        {
            this.Log("Muyu Disconnected");
            this.AddDM("Muyu Disconnected", true);
        }

        private void Class1_Connected(object sender, BilibiliDM_PluginFramework.ConnectedEvtArgs e)
        {
            //初始化一个客户端
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
            // 您的AccessKey ID。
            config.AccessKeyId = "LTAI5tSH8CtpyYMPQipMewmt";
            // 您的AccessKey Secret。
            config.AccessKeySecret = "9y7LCx8kWdeiTayZ14z3wCT8BpSplT";
            // 您的可用区ID。
            config.RegionId = "cn-shanghai";
            mClient = new AlibabaCloud.SDK.Iot20180120.Client(config);

            this.Log("Muyu Connected");
            this.AddDM("Muyu Connected", true);
        }

        public override void Admin()
        {
            base.Admin();
            Console.WriteLine("Hello World");
            this.Log("Hello World");
            this.AddDM("Hello World", true);
        }

        public override void Stop()
        {
            base.Stop();
            //請勿使用任何阻塞方法
            Console.WriteLine("Plugin Stoped!");
            this.Log("Plugin Stoped!");
            this.AddDM("Plugin Stoped!", true);
        }

        public override void Start()
        {
            base.Start();
            //請勿使用任何阻塞方法
            Console.WriteLine("Plugin Started!");
            this.Log("Plugin Started!");
            this.AddDM("Plugin Started!", true);
        }
    }
}
